# coding=utf-8
from conans import ConanFile, CMake, tools
import os
import sys
import os
sys.path.append(os.environ.get('PYTHONPATH'))
from pathlib import Path

class JsoncppConan(ConanFile):
    name = "libcurl"
    license = "MIT"
    homepage = " https://github.com/curl/curl.git"
    url = "https://gitee.com/tkxiong/curl.git"
    topics = ("conan", "json", "parser", "config")
    description = "Curl is a command-line tool for transferring data specified with URL syntax. F."
    settings = "os", "compiler", "arch", "build_type"
    options = {"shared": [True, False], "fPIC": [True, False],"debug":[True,False]}
    default_options = {"shared": False, "fPIC": True,"debug":True}
    exports_sources = ["CMakeLists.txt", "patches/**"]
    generators = "cmake"
    version = "1.1.1"

    def source(self):
        print("source  cwd is", os.getcwd())
        print("sys path is", sys.path)

        my_file = Path("./pkgsrc")
        if my_file.exists():
            print("not need to update source")
        else:
            print("source  cwd is", os.getcwd())
            git = tools.Git(folder="./pkgsrc", verify_ssl=False)
            git.clone(self.url)

    def build(self):
        print("path " + self.package_folder + " is where the package built")
        try:
            os.remove(tools.get_env("CONAN_SH_PATH"))  # path是文件的路径，如果这个路径是一个文件夹，则会抛出OSError的错误，这时需用用rmdir()来删除
        except:
            print("")
        #print("cmake module is " + tools.get_env("CMAKE_MODULE_PATH"))
        print(tools.get_env("BUILD_ENV"))
        if tools.os_info.is_windows:
            print("current os windows")
            if tools.os_info.is_cygwin:
                print("cygwin build")
            if tools.get_env("BUILD_ENV") == "msys":
                print("msys build")
                tools.run_in_windows_bash(self, subsystem="msys", cwd="pkgsrc", bashcmd="cmake  -DCMAKE_MODULE_PATH=" + tools.unix_path(self.source_folder) + " -G\"MinGW Makefiles\" " + tools.unix_path(self.source_folder) + "//pkgsrc")
                tools.run_in_windows_bash(self, subsystem="msys", cwd="pkgsrc", bashcmd="cmake --build .")
            if tools.get_env("BUILD_ENV") == "msvc64":
                print("msvc64 build")
                env_vars = tools.vcvars_dict(self)
                with tools.environment_append(env_vars):
                    self.run("chdir")
                    self.run("buildconf.bat",cwd="pkgsrc")
                    self.run("nmake -f Makefile.vc mode=static MACHINE=x64  vc=16 DEBUG=no ",
                    cwd="pkgsrc/winbuild")
                # self.run("cmake --build .",cwd="pkgsrc")

        if tools.os_info.is_linux:
            print("current os linux")
            self.run("cmake  -DEVENT__DISABLE_MBEDTLS=ON -DCMAKE_MODULE_PATH=" + tools.unix_path(self.source_folder) + " -G\"Unix Makefiles\" " + tools.unix_path(self.source_folder) + "/pkgsrc",cwd="pkgsrc")
            self.run("cmake --build .",cwd="pkgsrc")
            
    def package(self):
        if tools.os_info.is_windows:
            self.copy("pkgsrc\\build\\include\\json\\*", dst="include", keep_path=True)
            self.copy("pkgsrc\\build\\lib\\*", dst="lib", keep_path=False)
            self.copy("pkgsrc\\build\\bin\\*", dst="bin", keep_path=False)

    def requirements(self):
        if tools.os_info.is_windows:
            self.requires("nasm/2.15.05")
            if tools.get_env("BUILD_ENV") == "msys":
                self.requires("msysperl/5.30.0.1")
            if tools.get_env("BUILD_ENV") == "msvc32":
                self.requires("strawberryperl/5.30.0.1")
            if tools.get_env("BUILD_ENV") == "msvc64":
                self.requires("strawberryperl/5.30.0.1")      
